library(tidyverse)
library(sf)
library(raster)
library(leaflet)
library(ggspatial)
library(rnaturalearth)
library(rnaturalearthdata)
library(maps)
library(osrm)
library(tidygeocoder)
data <- read_rds("../data/data_aggregated.rds")
head(data)
unique(data$PR_NAME)
[1] "Western Cape" "Eastern Cape" "Northern Cape" "Gauteng"
#select and filter
data_geo <- data %>%
#filter(`PR_NAME` %in% c("Western Cape")) %>%
dplyr::select(`timestamp`, `vehicleid`, `event_description`, `coordinate_latitude`, `coordinate_longitude`) %>%
mutate(icon =
case_when(
event_description == "Power On" ~ "car-on",
event_description == "Idle Start" ~ "hand",
event_description == "ROAM_Start" ~ "satellite-dish",
event_description == "Speeding" ~ "gauge-high",
event_description == "Speeding end" ~ "gauge",
event_description == "GPS_Lock" ~ "location-crosshairs",
event_description == "GPS_Lost" ~ "circle-xmark")) %>%
drop_na(icon)
data_geo_speeding <- data_geo %>%
filter(event_description %in% c("Speeding"))
data_geo_GPS <- data_geo %>%
filter(event_description %in% c("GPS_Lock","GPS_Lost"))
data_spatial <- st_as_sf(
data_geo,
coords = c("coordinate_longitude","coordinate_latitude"),
crs = 4326
)
data_geo_speeding <- st_as_sf(
data_geo_speeding,
coords = c("coordinate_longitude","coordinate_latitude"),
crs = 4326
)
data_geo_GPS <- st_as_sf(
data_geo_GPS,
coords = c("coordinate_longitude","coordinate_latitude"),
crs = 4326
)
# route code - slightly buggy
# data_spatial <- data_spatial %>%
# group_by(vehicleid) %>%
# arrange(timestamp) %>%
# summarise(do_union = F)
#
# routes_spatial <- data_spatial %>%
# st_cast(to = "LINESTRING")
#
# routes_spatial = routes_spatial %>%
# mutate(distance = st_length(.))
world <- ne_countries(country = "South Africa", returnclass = "sf") %>% st_set_crs(4326)
colnames(data_geo_events)
Error in is.data.frame(x) : object 'data_geo_events' not found
# GPS Lost/Lock